import { Injectable } from '@angular/core';
import { AppConfig } from "../config";
import { Http, Headers, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Observable";
import { DatePipe } from "@angular/common";
import { PowerLoss } from "../entity/power-loss.model";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class PowerLossRankService {

  private fomat: string = "yyyy-MM-dd";
  private readonly baseUrl = `${AppConfig.APP_RESTFUL}`;

  private readonly paramF = "powerLosses/getPowerLossRankByAllFarms";
  private readonly urlF = `${this.baseUrl}/${this.paramF}`;

  private readonly paramT = "powerLosses/getPowerLossRankByAllturbines";
  private readonly urlT = `${this.baseUrl}/${this.paramT}`;

  private readonly paramFid = "powerLosses/getPowerLossRankByFarmturbines";
  private readonly urlFid = `${this.baseUrl}/${this.paramFid}`;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe) { }

  /**
  * 获取所有风场的损失电量集合
  * 
  * @returns {Promise<PowerLoss[]>} 
  * @memberof PowerLossRankService
  */
  getFarmPowerLossRankList(farmId: number[], powerLossType: number, dateType: number, date: Date): Promise<PowerLoss[]> {
    let dateFomat: string = "";
    dateFomat = this.datePipe.transform(date, this.fomat);
    //'http://192.168.15.150:8585/webapi/v1/powerLosses/getPowerLossRankByAllFarms'
    return this.http.post(this.urlF, JSON.stringify({ 'farmId': farmId,'powerLossType': powerLossType, 'dateType': dateType, 'date': dateFomat}))
      .toPromise()
      .then(response => {
        let resault = response.json();
        try {
          switch (resault.code) {
            case "200":
              return resault.data as PowerLoss[];
            case "-1":
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  /**
  * 获取所有风机的排名数据
  * 
  * @returns {Promise<PowerLoss[]>} 
  * @memberof PowerLossRankService
  */
  getTurbinePowerLossRankList(farmId: number[], powerLossType: number, dateType: number, date: Date, rankType:number, rankValue: number): Promise<PowerLoss[]> {

    let dateFomat: string = "";
    dateFomat = this.datePipe.transform(date, this.fomat);
    //'http://192.168.15.150:8585/webapi/v1/powerLosses/getPowerLossRankByAllturbines'

    return this.http.post(this.urlT, JSON.stringify({ 'farmId': farmId,'powerLossType': powerLossType, 'dateType': dateType, 'date': dateFomat, 'rankType':rankType, 'rankValue': rankValue} ))
      .toPromise()
      .then(response => {
        let resault = response.json();
        try {
          switch (resault.code) {
            case "200":
              return resault.data as PowerLoss[];
            case "-1":
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  /**
  * 获取当前风场下的风机损失电量排名集合
  * 
  * @returns {Promise<PowerLoss[]>} 
  * @memberof PowerLossRankService
  */
  getTurbinePowerLossRankListByFarmId(farmId: number[], powerLossType: number, dateType: number, date: Date, rankType:number, rankValue: number): Promise<PowerLoss[]> {

    let dateFomat: string = "";
    dateFomat = this.datePipe.transform(date, this.fomat);
    //'http://192.168.15.150:8585/webapi/v1/powerLosses/getPowerLossRankByFarmturbines'

    return this.http.post(this.urlFid, JSON.stringify({ 'farmId': farmId,'powerLossType': powerLossType, 'dateType': dateType, 'date': dateFomat, 'rankType':rankType, 'rankValue': rankValue} ))
      .toPromise()
      .then(response => {
        let resault = response.json();
        try {
          switch (resault.code) {
            case "200":
              return resault.data as PowerLoss[];
            case "-1":
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }
}



// WEBPACK FOOTER //
// ./src/app/service/power-loss-rank.service.ts